// pages/medical/myorder/index.js
import {
  hospitalApi,
  checkUserIsExist,
  cancelReservation
} from "../../../utils/hospital-api";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: wx.getStorageSync('userInfo'),
    list: [],
    status: {
      0: '已完成',
      1: '待支付',
      2: '待就诊',
      3: '患者违约',
      4: '患者退约',
      5: '支付失败无效',
      6: '被医院取消',
    },
    statusTag: {
      0: '#07c160',
      1: '#ff976a',
      2: '#ff976a',
      3: '#ee0a24',
      4: '#ee0a24',
      5: '#ccc',
      6: '#ccc',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.checkUserIsExist();
  },
  // 检查用户是否存在
  checkUserIsExist() {
    const that = this;
    checkUserIsExist().then(res => {
      // 存在的情况下
      if (wx.getStorageSync('wjwPassword')) {
        this.getOrderList(wx.getStorageSync('wjwPassword'));
      } else {
        wx.showModal({
          title: '请输入密码',
          content: '',
          placeholderText: '请输入南京12320平台的密码',
          editable: true,
          success(res) {
            if (res.confirm) {
              that.getOrderList(res.content);
            } else if (res.cancel) {
              wx.navigateBack()
            }
          }
        })
      }
    })
  },
  getOrderList(wjwPassword) {
    const that = this;
    hospitalApi({
      method: 'getReservation',
      data: JSON.stringify({
        checkId: this.data.user.idNumber,
        password: wjwPassword
      })
    }).then(res => {
      if(res.code==1000){
        // 如果获取数据成功 就把密码存下来
        wx.setStorageSync('wjwPassword', wjwPassword)
        this.setData({
          list: res.data
        })
        wx.hideLoading()
      }else{
        wx.showModal({
          title: '提示',
          content: res.data.MSG,
          showCancel:false,
          success(res){
            if(res.confirm){
              wx.showModal({
                title: '请输入密码',
                content: '',
                placeholderText: '请输入南京12320平台的密码',
                editable: true,
                success(res) {
                  if (res.confirm) {
                    that.getOrderList(res.content);
                  } else if (res.cancel) {
                    wx.navigateBack()
                  }
                }
              })
            }
          }
        })
      }
    })
  },
  // 取消预约
  cancelReservation(e) {
    const item = e.mark.item;
    const that = this;
    wx.showModal({
      title: '提示',
      content: '确定取消预约吗？取消后不能恢复！',
      success(res) {
        if (res.confirm) {
          cancelReservation({
            method: 'cancelReservation',
            data: JSON.stringify({
              checkId: that.data.user.idNumber,
              password: wx.getStorageSync('wjwPassword'),
              idNum: that.data.user.idNumber,
              reservcode: item.RESERVCODE,
              path: 'wap',
              priIdNum: that.data.user.idNumber,
            })
          }).then(res => {
            if (res.code == 1000) {
              wx.showToast({
                title: '取消成功',
                icon: 'success',
                duration: 2000
              })
              that.getOrderList(wx.getStorageSync('wjwPassword'))
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})